!function () {
    var zyList = [],
       zyList3=[],
       type,
       xiangqArr,
       typeArr;
    var init = function () {
        initEvent();
        getDate();
      
        getType();   
    }
    var initEvent = function () {   
        $('.zyArr').on('click','li',OnAclick);
    }

    //二级详情页列表的点击
    var  OnAclick  =function(e){
        var i=e.currentTarget.dataset.p;
        // var $this=$(this);
        // console.log(1);
        localStorage.setItem('zylistDate',JSON.stringify(xiangqArr[i]));
        location.href='../home/detail2.html';
    }
     
    
    //存一个自定义属性
    var saveData=function(){
        localStorage.setItem('zylist',JSON.stringify(zyList));
        // localStorage.setItem('index',JSON.stringify($("#get").data('p')));
    }

    //渲染二级详情菜单的
    var renderList = function () {
    
        var $zyArr = [],
        $chooseListArr=[];
        var curType='';
        console.log(typeArr);
        typeArr.forEach(function(item){
            if(item.name==type){
                curType=item.id
            }
        })
        console.log(zyList);
     xiangqArr=zyList.filter(function(item){
             return item.type==curType
       })

        $.each(xiangqArr, function (i,o) {
            $zyArr.push('<li data-p=',i,'>',
            '<a >[',o.title,']</a>',
            '<a class="overflow">',o.content,'</a>',
            '<span class="vermiddle">',
            '<img src="http://www.laizhouba.com/newtemplate/images/zhiding_new.png" alt="">',
            '</span>',
            '<span class="date">[',o.date.substr(0,10),']</span>',
               '<span class="area">3室2厅</span>',
            '<span class="area">',o.area,'</span>',
        '</li>')
        })
        $('.zyArr').html($zyArr.join(''));

    //渲染推荐的
    // var zyList2=zyList.filter(function(item){
    //       return item.id<5;
    //   })
     
        $.each(xiangqArr,function(i,o){
            $chooseListArr.push('<li>',
            '<span class="jian">&nbsp;荐</span>',
            '<a href="">[',o.title,']</a>',
            '<a href="">',o.content,'</a>',
            '<span class="date">[',o.date.substr(0,10),']</span>',
        '</li>')
        })
        $('.choose').html($chooseListArr.join(''));
    }

    //处理二级菜单的
    var delerji=function(yiji){
        var erjiArr = []
        zyList3.forEach(function(item){
            item.belong==yiji?erjiArr.push(item):null;
        })
        var lis = ''
        erjiArr.forEach(function(item){
            lis = lis + ('<a>'+item.name+'</a>')
        })
        return lis;
    }

    //得到链接地址上的参数
    var getType=function(){
      type=decodeURIComponent(location.search.substr(6))  ;
  
    }
    //渲染分类列表的
    var renderFenlei=function(){
        $fenleiArr=[];
            $fenleiArr.push(delerji(type));
        $('.fenlei').append($fenleiArr.join('')) 
    }

    //得到二级菜单的数据
    var gerTypeDate=function(){
        $.ajax({
            url: "http://localhost/api.php/laizhou/type?transform=1",
            type: "get",
            success: function (res) {
                typeArr = res.type
                zyList3 = typeArr.filter(function(item){
                    return item.level == '二级菜单'
                })
                renderFenlei();
                renderList();
            }
        })
    }
    //得到zydata数据
    var getDate = function () {
        $.ajax({
            url: "http://localhost/api.php/laizhou/zydata?transform=1",
            type: "get",
            success: function (res) {
                zyList = res.zydata;
                // renderList();
                saveData();
                gerTypeDate();
                // renderList();
            }
        })
    }
    init();
}()
